# encoding: utf-8
# @File  : conftest.py
# @Author: wu shaofan
# @Date  :  2024/04/28
# @Desc :
import allure
import pytest


driver = None
# 定义 pytest_runtest_makereport 钩子函数，用于在测试用例失败时添加异常截图到 Allure 报告
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    # 执行所有其他钩子以获取报告对象
    outcome = yield
    rep = outcome.get_result()
    if rep.when == "call" and rep.failed:
        # 如果 driver 对象存在且有 get_screenshot_as_png 方法，则添加异常截图到 Allure 报告
        if hasattr(driver, "get_screenshot_as_png"):
            allure.attach(driver.get_screenshot_as_png(), "用例异常截图", allure.attachment_type.PNG)